package cn.com.swu.mianyangapp.ui.news;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import cn.com.swu.mianyangapp.R;
import cn.com.swu.mianyangapp.ui.bean.News;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class NewsHomeListAdapter extends RecyclerView.Adapter<NewsHomeListAdapter.NewsListViewHolder> {
    final private String mIp = "192.168.58.15:8081";
    private List<News> mNewsList = new ArrayList<>();;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private Handler mHandler;


    String Tag = "OKHttp_ListItem";
    public NewsHomeListAdapter(Context context, List<News> newsList){
        this.mContext = context;
        this.mNewsList = newsList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }


    @NonNull
    @Override
    public NewsListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.fragment_news_list_item,parent,false);
        NewsListViewHolder newsListViewHolder = new NewsListViewHolder(view);
        return newsListViewHolder;
    }

    @Override
    @SuppressLint("HandlerLeak")
    public void onBindViewHolder(@NonNull NewsListViewHolder holder, int position) {
        News news = mNewsList.get(position);
        holder.mTvTitle.setText(news.getNews_name());
        if(news.getNews_top() == 1){holder.mTvTitle.setTextColor(Color.parseColor("#FF0000"));}

        holder.mTvAuthor.setText(news.getNews_time());
        if (news.getNews_image_name() != null) {
            getBitImage(news.getNews_image_name());
            mHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    Log.e(Tag, msg.toString());
                    Log.e(Tag, news.getNews_name());
                    Bitmap bitmap = (Bitmap) msg.obj;
                    holder.mImageView.setImageBitmap(bitmap);
                }
            };
        }
        else {
            holder.mImageView.setVisibility(View.GONE);
        }
        holder.mRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), NewsAddActivity.class);
                intent.putExtra("News",news);
                v.getContext().startActivity(intent);
            }
        });


    }

    private void getBitImage(String news_image_name) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://" + mIp +"/"+news_image_name)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                Log.e(Tag,e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                InputStream inputStream = response.body().byteStream();//得到图片的流
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                Message msg = new Message();
                msg.obj = bitmap;
                mHandler.sendMessage(msg);

            }
        });
    }


    @Override
    public int getItemCount() {
        return mNewsList.size();
    }

    class NewsListViewHolder extends RecyclerView.ViewHolder{
        TextView mTvTitle;
        TextView mTvAuthor;
        ImageView mImageView;
        RelativeLayout mRelativeLayout;


        public NewsListViewHolder(@NonNull View itemView) {
            super(itemView);
            this.mImageView = itemView.findViewById(R.id.news_image);
            this.mTvAuthor = itemView.findViewById(R.id.news_author);
            this.mTvTitle = itemView.findViewById(R.id.news_title);
            this.mRelativeLayout = itemView.findViewById(R.id.news_item);
        }
    }


}
